<?php
// +----------------------------------------------------------------------
// | 跃飞 [ 将来的你一定会感激现在奋斗的自己 ]
// +----------------------------------------------------------------------
// | Author: 钟贵廷
// +----------------------------------------------------------------------
// | Author URI: https://git.oschina.net/yeafy
// +----------------------------------------------------------------------
// | weChat:gt845272922  qq:845272922
// +----------------------------------------------------------------------

namespace app\home\controller;
use app\yeafy\abstractFactory\demo1\KetchupPizzaFactory;
use app\yeafy\abstractFactory\demo1\SaladPizzaFactory;
use app\yeafy\factoryMethod\demo1\CavalryFactory;
use app\yeafy\factoryMethod\demo1\InfantryFactory;
use app\yeafy\factoryMethod\demo2\ChinesePizzaFactory;
use app\yeafy\factoryMethod\demo2\USPizzaFactory;
use app\yeafy\simpleFactory\demo2\PizzaFactory;
use think\Controller;
use app\yeafy\strategy\demo1\ContextItem;
use app\yeafy\simpleFactory\demo1\ProductFactory;
use think\Db;

class Test extends Controller
{
    /**
     * 描述:策略者设计模式测试
     * @author 钟贵廷
     * @WeChat gt845272922
     * @qq 845272922
     */
    public function test1()
    {
        $obj = new ContextItem();
        //$obj->getItem('app\yeafy\strategy\AItem','Algorithm');
        $obj->getItem('app\yeafy\strategy\demo1\BItem','Behavior');
    }

    /**
     * 描述:策略模式之三国
     * @author 钟贵廷
     * @WeChat gt845272922
     * @qq 845272922
     */
    public function test2()
    {
        $obj = new ContextItem();
        $obj->getItem('app\yeafy\strategy\demo2\BackDoor','operate');
        echo "<br/>";
        $obj->getItem('app\yeafy\strategy\demo2\BlackEnemy','operate');
        echo "<br/>";
        $obj->getItem('app\yeafy\strategy\demo2\GivenGreenLight','operate');
    }

    /**
     * 描述:设计模式之简单工厂
     * @author 钟贵廷
     * @WeChat gt845272922
     * @qq 845272922
     */
    public function test3()
    {
        $obj = array();
        $obj[] = ProductFactory::getInstance(1); // 生产一个步兵
        $obj[] = ProductFactory::getInstance(2); // 生产一个骑兵
        $obj[] = ProductFactory::getInstance(3); // 生产错了
        $obj[] = ProductFactory::getInstance(2); // 生产一个骑兵
        foreach ($obj as $k=>$v)
        {
            if($v)
            {
                $v->attack();
                $v->defense();
            }
            else
            {
                echo '暂无该类兵种！<br/>';
            }
        }
    }

    /**
     * 描述:设计模式之工厂方法
     * @author 钟贵廷
     * @WeChat gt845272922
     * @qq 845272922
     */
    public function test4()
    {
        $obj = [];
        // 生产一个步兵
        $obj[]  = CavalryFactory::getInstance();
        $obj[] =  InfantryFactory::getInstance();
        $obj[] =  CavalryFactory::getInstance();
        foreach ($obj as $k=>$v)
        {
            $v->attack();
        }
    }


    /**
     * 描述:使用简单工厂设计模式生产披萨
     * @author 钟贵廷
     * @WeChat gt845272922
     * @qq 845272922
     */
    public function test5()
    {
        $pizza = PizzaFactory::getInstance('US');
        if($pizza)
        {
            $pizza->addFlour();
            $pizza->addWater();
        }
    }

    /**
     * 描述:使用工厂方法设计模式生产披萨
     * @author 钟贵廷
     * @WeChat gt845272922
     * @qq 845272922
     */
    public function test6()
    {
        $chinesePizza = new ChinesePizzaFactory();
        $chinesePizza->createPizza();
        $usPizza = new USPizzaFactory();
        $usPizza->createPizza();
    }

    /**
     * 描述:抽象工厂设计模式之生产多种口味的披萨
     * @author 钟贵廷
     * @WeChat gt845272922
     * @qq 845272922
     */
    public function test7()
    {
        $kcp = new KetchupPizzaFactory();
        $kcp->createUSPizza();
        $kcp->createChinesePizza();

        $spf = new SaladPizzaFactory();
        $spf->createUSPizza();
        $spf->createChinesePizza();
    }


    public function test8()
    {

        Db::name('aaa')->select();
    }
}
